import 'package:flutter/material.dart';
import 'package:testflutte/relsease/Utils.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:rxdart/rxdart.dart';
import 'package:testflutte/relsease/utils/SpUtil.dart';
import 'package:common_utils/common_utils.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SplashPageState();
  }
}

class SplashPageState extends State<SplashPage> {
  List<Widget> _bannerList = new List(); //存储闪动的滑动的页面
  int _status = 0; //判断当前显示哪部分
  TimerUtil _timerUtil; //倒计时类
  int _count = 3; //倒计时时间

  @override
  void initState() {
    super.initState();
    _initAsync();
  }

  void _initAsync() async {
    await SpUtil.getInstance();
    Observable.just(1).delay(Duration(milliseconds: 500)).listen((_) {
      _initSplash();
    });
  }

  void _initSplash() {
    for (int i = 0, length = 5; i < length; i++) {
      if (i % 2 == 0) {
        _bannerList.add(Image.asset(
          Utils.getimagePath('splash_bg'),
          fit: BoxFit.fill,
        ));
      } else {
        _bannerList.add(Image.network(
            'http://n.sinaimg.cn/sports/2_img/upload/cf0d0fdd/107/w1024h683/20181128/pKtl-hphsupx4744393.jpg',
            fit: BoxFit.fill));
      }
    }
    setState(() {});
    _doCountDown();
  }

  //倒计时
  void _doCountDown() {
    _timerUtil = TimerUtil(mTotalTime: 3 * 1000);
    _timerUtil.setOnTimerTickCallback((int tick) {
      double _tick = tick / 1000;
      setState(() {
        //每变一次更新一次数据
        _count = _tick.toInt();
      });
      if (_tick == 0) {
        //跳转到主页
        Navigator.of(context).pushReplacementNamed('/MainPage');
      }
    });
    _timerUtil.startCountDown();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    var width = size.width;
    var height = size.height;

    return Material(
      child: Stack(
        children: <Widget>[
          Offstage(
            offstage: true,
            child: Container(
              width: width,
              height: height,
//              child: Image.asset(Utils.getimagePath("splash_bg")),
              child: Image.network(
                'http://n.sinaimg.cn/sports/2_img/upload/cf0d0fdd/107/w1024h683/20181128/pKtl-hphsupx4744393.jpg',
                fit: BoxFit.fitHeight,
              ),
            ),
          ),
          //广告显示
          Offstage(
            offstage: false,
            child: Container(
              height: height,
              child: Swiper(
                itemCount: _bannerList.length,
                itemBuilder: _swiperBuilder,
                //分页指示器
                pagination: new SwiperPagination(
                    builder: DotSwiperPaginationBuilder(
                  color: Colors.black54,
                  activeColor: Colors.blue,
                )),
//              control: new SwiperControl(),
                //滚动方向
                scrollDirection: Axis.horizontal,
                //是否自动播放
                autoplay: true,
                //无限轮播
                loop: true,
                //自动播放延迟毫秒数.
                autoplayDelay: 3000,
                //当用户拖拽的时候，是否停止自动播放.
                autoplayDisableOnInteraction: true,
                //动画时间
                duration: 300,
                onTap: (index) => print('点击了第$index个'),
              ),
            ),
//
          ),
          //倒计时控件
          Offstage(
            offstage: false,
            child: Container(
                alignment: Alignment.bottomRight,
                margin: EdgeInsets.only(bottom: 20.0, right: 20.0),
                child: InkWell(
                  onTap: () {},
                  child: Container(
                    padding: EdgeInsets.all(5.0),
                    child: Text(
                      '跳过$_count',
                      style: TextStyle(fontSize: 12.0, color: Colors.white),
                    ),
                    decoration: BoxDecoration(
                        color: Color(0x66000000),
                        borderRadius: BorderRadius.all(Radius.circular(4.0)),
                        border: new Border.all(width: 0.33, color: Color(0xffe5e5e5))),
                  ),
                )),
          )
        ],
      ),
    );
  }

  Widget _swiperBuilder(BuildContext context, int index) {
    return _bannerList[index];
  }
}
